<?php

namespace app\store\controller\goods;

use app\store\controller\BaseController;
use app\store\model\Goods;
use app\store\service\GoodsCategoryService;
use app\store\service\GoodsService;
use think\facade\View;
use think\exception\ValidateException;
use app\store\service\GoodsTagService;
use app\store\service\StoreUserGradeService;

class GoodsController extends BaseController
{
    public function index()
    {   
        $param = $this->request->get();

        $goodsCategoryService = new GoodsCategoryService();
        $options = $goodsCategoryService->getCategoryOption(true);

        $goodsService = new GoodsService();
        $list = $goodsService->getGoodsList($param);


        return View::fetch('',['options' => $options, 'list' => $list]);
    }
    
    public function create()
    {
        $id = $this->request->get('id/d',false);

        $goodsCategoryService = new GoodsCategoryService();
        $categoryOptions = $goodsCategoryService->getCategoryOption(true);

        $tagOptions = GoodsTagService::getGoodsTagOptions();

        $gradeOptions = StoreUserGradeService::getStoreUserGradeOptions();

        if (!$id){
            return View::fetch('',[
                'categoryOptions' => $categoryOptions,
                'tagOptions' => $tagOptions,
                'gradeOptions' => $gradeOptions
            ]);
        }

        $goodsService = new GoodsService();
        $row = $goodsService->findGoods($id);

        if(!empty($row->items)){
            $row = $row->toArray();
            $row['items'] = json_encode($row['items'],JSON_UNESCAPED_UNICODE);
            $row['tags'] = array_column($row['tags'],'id');
        }


        return View::fetch('',[
            'categoryOptions' => $categoryOptions,
            'tagOptions' => $tagOptions,
            'gradeOptions' => $gradeOptions,
            'row' => $row
        ]);
    }

    public function option()
    {   
        $param = $this->request->param(); 

        $param['type'] = !empty($param['type']) ? 1 : 0;

        $goodsCategoryService = new GoodsCategoryService();
        $options = $goodsCategoryService->getCategoryOption(true);

        $goodsService = new GoodsService();
        $list = $goodsService->getGoodsOptions($param);

        View::engine()->layout(false);


        return View::fetch('',['type' => $param['type'], 'options' => $options, 'list' => $list]);
    }



    public function edit()
    {   
        $id = $this->request->get('id/d');

        $goodsCategoryService = new GoodsCategoryService();
        $categoryOptions = $goodsCategoryService->getCategoryOption(true);

        $tagOptions = GoodsTagService::getGoodsTagOptions();

        $gradeOptions = StoreUserGradeService::getStoreUserGradeOptions();

        $goodsService = new GoodsService();
        $row = $goodsService->findGoods($id);

        if(!empty($row->items)){
            $row = $row->toArray();
            $row['items'] = json_encode($row['items'],JSON_UNESCAPED_UNICODE);
            $row['tags'] = array_column($row['tags'],'id');
        }
 
        return View::fetch('',['categoryOptions' => $categoryOptions, 'tagOptions' => $tagOptions, 'gradeOptions' => $gradeOptions,'row' => $row]);
    }


    public function save()
    {
        $param = $this->request->post();
        try {
            validate('GoodsValidate')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }
        $goodsService = new GoodsService();
        $id =  $goodsService->saveGoods($param);

        
        return $id ? json(['code' => 1, '保存成功!','url' => url('/store/goods')]) : json(['code' => 0, '保存失败!']);

    }

    public function switch()
    {
        $param = $this->request->post();

        try {
            validate('GoodsValidate.switch')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        Goods::update($param);

        return json(['code' => 1, 'msg' => '更新成功!']);
    }



    public function update()
    {
        $param = $this->request->post();

        try {
            validate('GoodsValidate.edit')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }
        
        $goodsService = new GoodsService();
        $id =  $goodsService->updateGoods($param);
        
        return $id ? json(['code' => 1, '保存成功!']) : json(['code' => 0, '保存失败!']);
    }

    public function recycle()
    {
       
        $param = $this->request->get();

        $goodsCategoryService = new GoodsCategoryService();
        $options = $goodsCategoryService->getCategoryOption(true);

        $goodsService = new GoodsService();
        $list = $goodsService->getGoodsRecycleList($param);

        if(!$this->request->isPjax()){
            View::engine()->layout(true);
        }

        return View::fetch('',['options' => $options, 'list' => $list]);
    }

    public function restore()
    {
       
        $id = $this->request->get('id/d');
        $goodsService = new GoodsService();

        $result = $goodsService->goodsRestore($id);


        return $result ?  json(['code' => 1, 'msg' => '恢复成功!']) : json(['code' => 0, 'msg' => '恢复失败!']);
    }



    public function delete()
    {
        $id = $this->request->get('id/d');
        $result = Goods::destroy($id);

        return $result ?  json(['code' => 1, 'msg' => '删除成功!']) : json(['code' => 0, 'msg' => '删除失败!']);
    }

    public function deleteTag()
    {
        $param = $this->request->param();
        GoodsService::deleteGoodsTag($param['goods_id'],$param['goods_tag_id']);

        return json(['code' => 0, 'msg' => '标签删除失败!']);
    }
}